﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Web.SystemConfig.WorkFlow
{
    public class WorkFlowMessageStatusCache
    {
        private static WorkFlowMessageStatusCache m_WorkFlowMessageStatusList=null;
        private List<WorkFlowMessageStatus> m_ArrayList = null;

        public WorkFlowMessageStatusCache()
        {
            if(this.m_ArrayList==null)
            {
                this.m_ArrayList = new List<WorkFlowMessageStatus>();
            }
        }

        public void GetInstance()
        {
            if(m_WorkFlowMessageStatusList==null)
            {
                m_WorkFlowMessageStatusList = new WorkFlowMessageStatusCache();
            }

            ///初始化消息状态信息
            DataTable dt = null;
            WorkFlowMessageStatus rule = null;
            string strMsg = new WorkFlowMessageStatusDAL().GetAllWorkFlowMessageStatus(out dt);
            if (string.IsNullOrEmpty(strMsg))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    rule = new WorkFlowMessageStatus();
                    rule.StatusID = int.Parse(dr["STATUSID"].ToString());
                    rule.StatusName = dr["STATUSNAME"].ToString();
                    rule.TableID = int.Parse(dr["TableID"].ToString());
                    rule.TableName = dr["TableName"].ToString();
                    rule.DefaultStatusID = int.Parse(dr["DefaultStatusID"].ToString());

                    m_ArrayList.Add(rule);
                }
            }
        }

        /// <summary>
        /// 获取业务表的初始状态
        /// </summary>
        /// <param name="tableID"></param>
        /// <returns></returns>
        public WorkFlowMessageStatus GetOrginalMessageStatus(ArchiveTableEnum tableEnum)
        {
            int tableID = (int)tableEnum;
            WorkFlowMessageStatus rtnStatus = new WorkFlowMessageStatus();
            foreach (WorkFlowMessageStatus msgStatus in this.m_ArrayList)
            {
                if (msgStatus.TableID == tableID && msgStatus.StatusID == msgStatus.DefaultStatusID)
                {
                    rtnStatus = msgStatus;
                    break;
                }
            }

            return rtnStatus;
        }

        /// <summary>
        /// 通过状态ID获取状态
        /// </summary>
        /// <param name="statusID"></param>
        /// <returns></returns>
        public WorkFlowMessageStatus GetMessageStatusByStatusID(int statusID)
        {
            WorkFlowMessageStatus rtnStatus = new WorkFlowMessageStatus();
            foreach (WorkFlowMessageStatus msgStatus in this.m_ArrayList)
            {
                if (msgStatus.StatusID == statusID)
                {
                    rtnStatus = msgStatus;
                    break;
                }
            }

            return rtnStatus;
        }

        /// <summary>
        /// 获取业务表名
        /// </summary>
        /// <param name="tableID"></param>
        /// <returns></returns>
        public string GetTableName(int tableID)
        {
            string rtnStr = string.Empty;
            foreach (WorkFlowMessageStatus msgStatus in this.m_ArrayList)
            {
                if (msgStatus.TableID == tableID)
                {
                    rtnStr = msgStatus.TableName;
                    break;
                }
            }
            return rtnStr;
        }

        /// <summary>
        /// 获取业务表的所有消息状态
        /// </summary>
        /// <param name="tableID"></param>
        /// <returns></returns>
        public List<WorkFlowMessageStatus> GetAllMessageStatus(int tableID)
        {
            List<WorkFlowMessageStatus> rtnList = new List<WorkFlowMessageStatus>();
            foreach (WorkFlowMessageStatus status in this.m_ArrayList)
            {
                if (status.TableID == tableID)
                {
                    rtnList.Add(status);
                }
            }
            return rtnList;
        }

        /// <summary>
        /// 备注：根据档案分类ID和流程名称取当前状态ID
        /// 时间：2012年12月13日 00:07:00
        /// </summary>
        /// <param name="tableId"></param>
        /// <param name="statusName"></param>
        /// <returns></returns>
        public int GetStatusIdByTableIdAndStatusName(int tableId, string statusName)
        {
            WorkFlowMessageStatus rtnStatus = new WorkFlowMessageStatus();
            foreach (WorkFlowMessageStatus msgStatus in this.m_ArrayList)
            {
                if (msgStatus.TableID == tableId && statusName == msgStatus.StatusName)
                {
                    rtnStatus = msgStatus;
                    break;
                }
            }
            return rtnStatus.StatusID;
        }
    }
}